package com.yuda.tools.checkanswer.controller;

import cn.hutool.extra.ssh.JschUtil;
import cn.hutool.extra.ssh.Sftp;
import com.jcraft.jsch.Session;
import com.yuda.tools.checkanswer.utils.DeploymentUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;

@RestController
public class DeploymentController {

    @GetMapping("/execDep")
    public void execDep() {
        Session session = null;
        Sftp sftp = null;
        try {
            session = JschUtil.getSession("182.92.96.88", 22, "root", "Thussat@200518");
            System.out.println("session.isConnected() ===  >  " + session.isConnected());
            System.out.println(JschUtil.exec(session, "kill -9  `ps -ef |grep modules-tools-checkanswer |awk '{print $2}'`", StandardCharsets.UTF_8));
            sftp = JschUtil.createSftp(session);
            DeploymentUtils.upload("E:\\newIdeaworkspace\\officialbackend\\modules\\modules-tools\\modules-tools-checkanswer\\target\\modules-tools-checkanswer.jar", "/mnt/aa/wyn/modules-tools-checkanswer.jar", sftp);
            System.out.println(JschUtil.exec(session, "pwd", StandardCharsets.UTF_8));
            System.out.println(JschUtil.exec(session, "nohup java -jar /mnt/aa/wyn/modules-tools-checkanswer.jar --spring.profiles.active=dev1  >/mnt/aa/wyn/checkanswer.out&", StandardCharsets.UTF_8));
            System.out.println("执行命令完毕...");
        } finally {

            DeploymentUtils.close(sftp);
            if (session != null) {
                JschUtil.close(session);
            }
        }
    }
}
